package com.gzhryc.email.dao;

import java.sql.SQLException;
import java.util.List;

import com.gzhryc.email.dao.db.InsideEmailUser;
import com.gzhryc.email.dao.models.InsideEmailEntity;
import com.gzhryc.jdbc.BaseDao;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Query;

/**
 * 用户网站内部邮件关系数据访问接口
 * @author 张浩
 * @version 2016-11-17
 */
public class InsideEmailUserDao extends BaseDao<InsideEmailUser>{
	
	static final String TYPE_CODE = "insideEmailType";

	public List<InsideEmailEntity> findEmailEntity(Conditions condition,Integer pageNo,Integer pageLimit) throws SQLException{
		String sql = "select t.*,t2.name as typeName,t1.status AS userEmailStatus from email_inside_email t"
				+ " inner join email_inside_email_user t1 on t1.email_id = t.id"
				+ " left join dic_enum_info t2 on t2.code = t.type and t2.parent_code = ?";
		Query query = new Query(sql);
		query.getParams().setObject(TYPE_CODE);
		query.appendSql(condition.toSql(query.getParams(), true));
		query.setPageNo(pageNo);
		query.setPageLimit(pageLimit);
		return this.jdbcHelper.find(query, InsideEmailEntity.class);
	}
	
	public Long countEmailEntity(Conditions condition) throws SQLException{
		String sql = "select count(*) from email_inside_email t"
				+ " inner join email_inside_email_user t1 on t1.email_id = t.id"
				+ " left join dic_enum_info t2 on t2.code = t.type and t2.parent_code = ?";
		Query query = new Query(sql);
		query.getParams().setObject(TYPE_CODE);
		query.appendSql(condition.toSql(query.getParams(), true));
		return this.jdbcHelper.getFirstField(query, Long.class);
	}
}
